/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package webClient;

import appClient.ClientCommand;
import appClient.ConnectClient;
import appClient.UserComman;
import com.google.gson.Gson;
import engine.Address;
import engine.City;
import engine.Country;
import engine.FullPersone;
import engine.GType;
import engine.Phone;
import engine.Singleton;
import engine.Street;
import engine.TypeAdress;
import engine.TypePhone;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import protocol.Command;
import webClient.HttpClient.SocketProcessor;

/**
 *
 * @author User
 */
class WebClientReaction {
  public SocketProcessor socproc;

  public WebClientReaction() {
  }
  

  public WebClientReaction(SocketProcessor socproc) {
    this.socproc = socproc;
  }

  public void getString(String res, SocketProcessor socproc) throws IOException, Throwable {
      this.socproc  = socproc;
      
    if (res.indexOf(".js") > 1) {
      loadJS(res);
    }  
    if (res.indexOf("login") > 1) {
      getLogandPass(res);
    }
    if (res.indexOf("persone") > 1) {
      getPersone(res);
    }
    if (res.indexOf("Save=Save") > 1) {
        savePerson(getForm(res));
    }
    if (res.indexOf("AddTel=AddTel") > 1) {
      addTel(getForm(res));
      loadPersone();
    }
    if (res.indexOf("AddAdr=AddAdr") > 1) {
      addAdr(getForm(res));
      loadPersone();
    }
    if (res.indexOf("DelTel=DelTel") > 1) {
      delTel(getForm(res), res);
      loadPersone();
    }
    if (res.indexOf("DelAdr=DelAdr") > 1) {
      delAdr(getForm(res), res);
      loadPersone();
    }
    if (res.indexOf("=GetCity") > 1) {
      getCity(getForm(res), res);
      loadPersone();
    }
    if (res.indexOf("=GetStreet") > 1) {
      getStreet(getForm(res), res);
      loadPersone();
    }
    if (res.indexOf("NeWCode") > 1) {
      emptySingleton();  
    }
    if (res.indexOf("AddInfoCode") > 1) {
      loadSInfo();  
    }
    if (res.indexOf("informationCode") > 1) {
      go3();  
    }
    
    if (res.indexOf("telt=telt") > 1) {
      loadTypeTel(res);  
      go3();  
    }
    if (res.indexOf("adrt=adrt") > 1) {
      loadTypeAdr(res);  
      go3();  
    }
    if (res.indexOf("countrt=countrt") > 1) {
      loadTypeCountry(res);  
      go3();  
    }
    if (res.indexOf("cityt=cityt") > 1) {
      loadTypeCity(res);  
      go3();  
    }
    if (res.indexOf("streett=streett") > 1) {
      loadTypeStreet(res);  
      go3();  
    }
    
    if (res.indexOf("upadteCode") > 1) {
     // getPersone(Singleton.fp.ID);
      loadPersone();
    }
    if (res.indexOf("formCode") > 1) {
      loadPersone();
    }
    if (res.indexOf("form.html ") > 1) {
      loadForm();
    } 
    if (res.indexOf("AddInfo.html ") > 1) {
      loadAddInfo();
    }        
    if (res.indexOf("Del=Del") > 1) {
      WebFunction.delPersone(Singleton.fp);
    } 
    
    
  }

  private void getLogandPass(String res) throws IOException, Throwable {
    String login = "";
    String pass = "";
    int end = res.indexOf("login");
    res  = res.substring(end + 6);
    end = res.indexOf("&");
    login  = res.substring(0, end);
    System.out.println(login);
    
    res.indexOf("pass");
    res  = res.substring(end + 6);
    end = res.indexOf("&");
    
    pass  = res.substring(0, end);
    System.out.println(pass);
    
    String st = socproc.wm.readFile("src/webClient/go.html");
    System.out.println(st + "d");
    socproc.writeResponse(st);
    
    UserComman.connection(login, pass, socproc);
    
  }

    private void getPersone(String res) {
        int end = res.indexOf("persone");
        res = res.substring(end + 8);
        end = res.indexOf(" ");
        String id  = res.substring(0, end);
        System.out.println(id);
        UserComman.getInfo(Singleton.cli, id);
    }

    private void loadPersone() throws FileNotFoundException, IOException, Throwable {
        String st = socproc.wm.readFile("src/webClient/go2.html");
        socproc.writeResponse(st);
    }
    
    private void go3() throws FileNotFoundException, IOException, Throwable {
        String st = socproc.wm.readFile("src/webClient/go3.html");
        socproc.writeResponse(st);
    }

    private void loadForm() throws FileNotFoundException, IOException, Throwable {
        FullPersone fp = Singleton.fp;
        String st = socproc.wm.readFile("src/webClient/form.html");
        st = st.replace("$FName", Singleton.fp.FName);
        st = st.replace("$LName", Singleton.fp.LName);
        st = st.replace("$BD", Singleton.fp.bd);
        st = WebFunction.setTels(st);
        st = WebFunction.setAdr(st);
        st = st.replace("$persone", "upadteCode");
        st +="</body></html>";
        String addInfo = "<input type=\"submit\" name=\"Save\" value=\"Save\" />";

        if (!Singleton.role.equals("Admin")) {
            if(Singleton.fp.ID != null){
                if (!Singleton.Uid.equals(Singleton.fp.ID)) {
                    st = st.replace(addInfo, "");
                }
            }
        }
        socproc.writeResponse(st);
    }
    
    private void loadAddInfo() throws FileNotFoundException, IOException, Throwable {
        FullPersone fp = Singleton.fp;
        String st = socproc.wm.readFile("src/webClient/AddInfo.html");
        
        st = st.replace("$TelType", formTel());
        st = st.replace("$AdrType", formAdr());
        st = st.replace("$Country", formCoutry());
        st = st.replace("$City", formCity());
        st = st.replace("$Street", formStreet());
        
        String addInfo = "<input type=\"submit\" name=\"countrt\" value=\"countrt\" />";
        String addInfo2 = "<input type=\"submit\" name=\"cityt\" value=\"cityt\" />";
        String addInfo3 = "<input type=\"submit\" name=\"streett\" value=\"streett\" />";

        if (Singleton.role.equals("SUser")) {
           st = st.replace(addInfo, "");
           st = st.replace(addInfo2, "");
           st = st.replace(addInfo3, "");
        }
     
        socproc.writeResponse(st);
    }
    
    private String formTel()
    {
        String type = "";
        for (TypePhone tp : Singleton.list) {
            type += "<tr><td>" + tp.Type + "</td></tr>";
        }
        return type;
    }
    
    private String formAdr()
    {
        String type = "";
        for (TypeAdress tp : Singleton.typeAdr) {
            type += "<tr><td>" + tp.Type + "</td></tr>";
        }
        return type;
    }
    
    private String formCoutry()
    {
        String type = "";
        for (Country tp : Singleton.country) {
            type += "<tr><td>" + tp.name + " - " + tp.ID + "</td></tr>";
        }
        return type;
    }
    
    private String formCity()
    {
        String type = "";
        for (City tp : Singleton.city) {
            type += "<tr><td>" + tp.name + " - " + tp.ID + "</td></tr>";
        }
        return type;
    }
    
    private String formStreet()
    {
        String type = "";
        for (Street tp : Singleton.street) {
            type += "<tr><td>" + tp.name + "</td></tr>";
        }
        return type;
    }
    
    private void loadJS(String res) {
        res = res.replace("GET /", "");
    }

    private FullPersone getForm(String res) {
        FullPersone fp = new FullPersone();
        System.out.println(res);
        fp.FName = getPars(res, "fname");
        fp.LName = getPars(res, "lname");
        fp.bd = getPars(res, "bd");
        fp.ID = Singleton.fp.ID;
        fp.uid = Singleton.fp.uid; /////////////////////!!!!!!!!!!!!
        fp.Phones = getPhones(res);
        fp.Addres = getAdres(res);
        return fp;
    }
    
    private void savePerson(FullPersone obj2)
    {
      Gson gson2 = new Gson();
      if (obj2.Phones.isEmpty()){
        obj2.Phones = null;
      }
      if (obj2.Addres.isEmpty()){
        obj2.Addres = null;
      }
      String str = gson2.toJson(obj2);
      Command obj = new Command();
      Gson gson = new Gson();
      obj.Type = "Set";
      GType gt = new GType();
      gt.Type = "Person";
      obj.Data = gson.toJson(gt);
      obj.Add = str;
      String res = gson.toJson(obj);
      Singleton.cli.refuse(res);
    }
    
    private String getPars(String str, String name)
    {
        String pars = str;
        int end = pars.indexOf(name);
        if(end != -1){
            pars = pars.substring(end + name.length() + 1);
            end = pars.indexOf("&");
            if (end == -1){
               end = pars.indexOf(" "); 
            }
            pars = pars.substring(0, end);
        } else {
            pars = "na";
        }
        
        System.out.println(pars);
        return pars;
    }

    private List<Phone> getPhones(String res) {
        List<Phone> listPhones = new ArrayList<Phone>();
        int count = 1;
        String tel = "23";
        while (!tel.equals("na"))
        {
            Phone ph = new Phone();
            tel = getPars(res, "telNumber" + count);
            String telID = getPars(res, "telID" + count);
            String telType = getPars(res, "telPype" + count);
            ph.Number = tel;
            ph.ID = telID;
            TypePhone tp = new TypePhone();
            tp.Type = telType;
            ph.Type = tp;
            if (!tel.equals("na")) {
                listPhones.add(ph);
            }
            count++;
        }
        return listPhones;
    }

    private List<Address> getAdres(String res) {
        List<Address> listAdress = new ArrayList<Address>();
        int count = 1;
        String tel = "23";
        String AdrId = "55";
        while (!AdrId.equals("na"))
        {
            Address ph = new Address();
            tel = getPars(res, "country" + count);
            String cit = getPars(res, "city" + count);
            String telType = getPars(res, "adrType" + count);
            String street = getPars(res, "street" + count); 
            String flat = getPars(res, "flat" + count); 
            AdrId = getPars(res, "AdrId" + count);        
            
            Country cn = new Country();
            cn.name = tel;
            ph.country = cn;
            
            City ct = new City();
            ct.name = cit;
            ph.city = ct;
            
            TypeAdress tp = new TypeAdress();
            tp.Type = telType;
            ph.Type = tp;
            
            Street st = new Street();
            st.name = street;
            ph.street = st;
            
            ph.Add = flat;
            ph.ID = AdrId;
            
            if (!tel.equals("na")) {
                listAdress.add(ph);
            }
            count++;
        }
        return listAdress;
    }

    private void addTel(FullPersone form) {
        Singleton.fp = form;
        Phone ph = new Phone();
        ph.Number = "Enter tel";
        ph.ID = "";
        TypePhone tp = new TypePhone();
        tp.Type = "";
        ph.Type = tp;
        Singleton.fp.Phones.add(ph);
    }

    private void addAdr(FullPersone form) {
        Singleton.fp = form;
        Address ph = new Address();
        
        Country cn = new Country();
        cn.name = "";
        ph.country = cn;

        City ct = new City();
        ct.name = "";
        ph.city = ct;

        TypeAdress tp = new TypeAdress();
        tp.Type = "";
        ph.Type = tp;

        Street st = new Street();
        st.name = "";
        ph.street = st;

        ph.Add = "";
        ph.ID = "";
        Singleton.fp.Addres.add(ph);
    }

    private void delTel(FullPersone form, String str) {
        Singleton.fp = form;
        String AdrId = "na";
        int count = 1;
        while (AdrId.equals("na"))
        {
            AdrId = getPars(str, "telCheck" + count);
            count++;
        }
        count = count -2;
        count = Integer.parseInt((Singleton.fp.Phones.get(count)).ID);
        Command obj = new Command();
        Gson gson = new Gson();
        obj.Type = "Del";
        GType gt = new GType();
        gt.Type = "Phone";
        obj.Data = gson.toJson(gt);
        obj.Add = gson.toJson(count);
        String res = gson.toJson(obj);
        Singleton.cli.refuse(res);
    }

    private void delAdr(FullPersone form, String str) {
        Singleton.fp = form;
        String AdrId = "na";
        int count = 1;
        while (AdrId.equals("na"))
        {
            AdrId = getPars(str, "checkAdr" + count);
            count++;
        }
        count = count -2;
        count = Integer.parseInt((Singleton.fp.Addres.get(count)).ID);
        Command obj = new Command();
        Gson gson = new Gson();
        obj.Type = "Del";
        GType gt = new GType();
        gt.Type = "Address";
        obj.Data = gson.toJson(gt);
        obj.Add = gson.toJson(count);
        String res = gson.toJson(obj);
        Singleton.cli.refuse(res);
    }

    public void getCity(FullPersone form, String res) {
        Singleton.fp = form;
        String AdrId = "na";
        int count = 1;
        while (AdrId.equals("na"))
        {
            AdrId = getPars(res, "GetCity" + count);
            count++;
        }
        count--;
        String country = getPars(res, "country" + count);
        
        String id = null;
        for (Country p : Singleton.country) {
            if (p.name.equals(country)) {
                id = p.ID;
            }
        }
        String go = UserComman.commandGet("City", id);
        Singleton.cli.refuse(go);
    }

    public void getStreet(FullPersone form, String res) {
        Singleton.fp = form;
        String AdrId = "na";
        int count = 1;
        while (AdrId.equals("na"))
        {
            AdrId = getPars(res, "GetStreet" + count);
            count++;
        }
        count--;
        String city = getPars(res, "city" + count);
        String id = null;
        for (City p : Singleton.city) {
            if (p.name.equals(city)) {
                id = p.ID;
            }
        }
        String go = UserComman.commandGet("Street", id); ////////////////ffd
        Singleton.cli.refuse(go);
    }

    private void emptySingleton() {
        Singleton.fp = new FullPersone();
        Singleton.fp.FName = "";
        Singleton.fp.LName = "";
        Singleton.fp.bd = "";
        Singleton.fp.Phones = new ArrayList<Phone>();
        Singleton.fp.Addres = new ArrayList<Address>();
        
    }

    private void loadSInfo() {
      ClientCommand.getTypePhone(Singleton.cli);
      ClientCommand.getTypeAdr(Singleton.cli);
      ClientCommand.getTypeCountry(Singleton.cli);
      GetCity(Singleton.cli);
      GetStreet(Singleton.cli);
    }
    
    private void GetCity(ConnectClient cli)
  {
    Command obj = new Command();
    Gson gson = new Gson();
    obj.Type = "Get";
    GType gt = new GType();
    gt.Type = "City";
    obj.Data = gson.toJson(gt);
    obj.Add = "-1";
    String res = gson.toJson(obj);
    cli.refuse(res);
  }
  
  private void GetStreet(ConnectClient cli)
  {
    Command obj = new Command();
    Gson gson = new Gson();
    obj.Type = "Get";
    GType gt = new GType();
    gt.Type = "Street";
    obj.Data = gson.toJson(gt);
    obj.Add = "-1";
    String res = gson.toJson(obj);
    cli.refuse(res);
  }
  
  private void commandSet(String str, Object ob)
  {
    Command obj = new Command();
    Gson gson = new Gson();
    obj.Type = "Set";
    GType gt = new GType();
    gt.Type = str;
    obj.Data = gson.toJson(gt);
    obj.Add = gson.toJson(ob);
    String res = gson.toJson(obj);
    Singleton.cli.refuse(res);
  }

    private void loadTypeTel(String res) {
        TypePhone tp = new TypePhone();
        tp.Type = getPars(res, "ftel");
        commandSet("TypePhone", tp);
    }
    
    private void loadTypeAdr(String res) {
        TypeAdress tp = new TypeAdress();
        tp.Type = getPars(res, "fadr");
        commandSet("TypeAdress", tp);
    }
    
    private void loadTypeCountry(String res) {
        Country tp = new Country();
        tp.name = getPars(res, "fcountr");
        commandSet("Country", tp);
    }
    
    private void loadTypeCity(String res) {
        City tp = new City();
        tp.name = getPars(res, "fcity");
        tp.OID = getPars(res, "countryID");
        commandSet("City", tp);
    }
    
    private void loadTypeStreet(String res) {
        Street tp = new Street();
        tp.name = getPars(res, "fstreet");
        tp.OID = getPars(res, "cityID");
        commandSet("Street", tp);
    }

    
  
}
